package com.zmx.user.service;

import com.zmx.common.common.exception.BusinessException;
import com.zmx.common.common.invocation.InvocationInfoProxy;
import com.zmx.common.common.service.BaseService;
import com.zmx.user.contents.ReceiverContents;
import com.zmx.user.dto.MemberReceiverDto;
import com.zmx.user.entity.MemberReceiver;
import com.zmx.user.repository.MemberReceiverRepository;
import com.zmx.user.service.mapper.MemberReceiverMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
@author zhaomxr
*/
@Service
public class MemberReceiverService extends BaseService<MemberReceiver, MemberReceiverDto, MemberReceiverRepository, MemberReceiverMapper>  {

    public List<MemberReceiverDto> getByMemberId() {
        List<MemberReceiverDto> receiverDTOList = null;

        List<MemberReceiver> memberReceivers = repository.getByMemberId(Long.valueOf(InvocationInfoProxy.getUserId()));
        return entityToDto(memberReceivers);
    }

    public MemberReceiverDto getReceiverById(Long id) {
        MemberReceiver memberReceiver = this.findById(id);
        return entityToDto(memberReceiver);
    }

    public MemberReceiverDto getReceiverDefault() {
        MemberReceiver memberReceiver = repository.getReceiverDefault(Long.valueOf(InvocationInfoProxy.getUserId()));
        return entityToDto(memberReceiver);
    }

    /**
     * 新增收货地址
     * @param memberReceiverDto
     */
    @Transactional(rollbackFor = Exception.class)
    public void addReceiver(MemberReceiverDto memberReceiverDto) {
        //检查地址
        checkReceiver(memberReceiverDto);
        memberReceiverDto.setId(null);

        //如果此次设置了新的默认地址，将之前收货地址全部置为普通收货地址
        Long memberId = Long.valueOf(InvocationInfoProxy.getUserId());
        if (memberReceiverDto.getIsDefault()) {
            repository.updateReceiverNoDefault(memberId);
        }

        memberReceiverDto.setCreator(String.valueOf(memberId));
        memberReceiverDto.setMemberId(memberId);
        this.create(memberReceiverDto);
    }

    /**
     * 修改收货地址
     * @param memberReceiverDto
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateReceiver(MemberReceiverDto memberReceiverDto) {
        //检查地址
        checkReceiver(memberReceiverDto);

        Long memberId = Long.valueOf(InvocationInfoProxy.getUserId());
        MemberReceiver memberReceiver = repository.findById(memberReceiverDto.getId());
        if (!Objects.equals(memberReceiver.getMemberId(), memberId)) {
            throw new BusinessException("更新地址和所属用户不一致");
        }

        //如果此次设置了新的默认地址，将之前收货地址全部置为普通收货地址
        if (memberReceiverDto.getIsDefault()) {
            repository.updateReceiverNoDefault(memberId);
        }

        memberReceiverDto.setModifier(String.valueOf(memberId));
        memberReceiverDto.setMemberId(memberId);
        this.update(memberReceiverDto);
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteReceiver(Long id) {
        Long memberId = Long.valueOf(InvocationInfoProxy.getUserId());

        MemberReceiver memberReceiver = repository.findById(id);
        if (!Objects.equals(memberReceiver.getMemberId(), memberId)) {
            throw new BusinessException("删除地址和所属用户不一致");
        }

        //此方法直接物理删除 收货地址，不可恢复
        this.delete(id);
    }

    private void checkReceiver(MemberReceiverDto memberReceiverDto) {
        if (memberReceiverDto == null || StringUtils.isEmpty(memberReceiverDto.getAddress()) || StringUtils.isEmpty(memberReceiverDto.getAreaName())) {
            throw new BusinessException(ReceiverContents.RECEIVER_ERROR_MSG);
        } else if (StringUtils.isEmpty(memberReceiverDto.getConsignee())) {
            throw new BusinessException(ReceiverContents.RECEIVER_CONSIGNEE_ERROR_MSG);
        } else if (StringUtils.isEmpty(memberReceiverDto.getPhone())) {
            throw new BusinessException(ReceiverContents.RECEIVER_PHONE_ERROR_MSG);
        }
    }
}
